package com.admin.education.controllers.api.v1;

import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.openId.OpenIdAuthorize;
import com.admin.education.common.utils.OpenIdUtil;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.user.Request.BindingInformationRequest;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.models.User;
import com.admin.education.services.Interfaces.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.Callable;

@RestController()
@RequestMapping(value = "v1/userInformations")
@OpenIdAuthorize
public class UserInformationController {
    @Autowired
    private IUserService userService;

    @GetMapping("")
    public Callable<IResponseMessage> getInformation(HttpServletRequest request) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                User user= OpenIdUtil.getUser(request);

                IResponseMessage responseMessage =null;
                ServicesExcuteResult<List<UserInformationTransfer>> result = userService.QueryUserInformations(user.getId());
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<List<UserInformationTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PostMapping("")
    public  Callable<IResponseMessage> createInformation(HttpServletRequest request,@RequestBody @Valid BindingInformationRequest bindingInformationRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                User user= OpenIdUtil.getUser(request);

                IResponseMessage responseMessage =null;
                ServicesExcuteResult result = userService.Binding(user.getId(),bindingInformationRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

}
